import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    alertText: "",
    alertType: "error",
    alertShow: false,
    wasteType:{
      '0':{
        name:'可回收垃圾',
        img:'recyclable_waste.png',
        logo:'recyclable_logo.png'
      },
      '1':{
        name:'有害垃圾',
        img:'hazardous_waste.png',
        logo:'hazardous_logo.png'
      },
      '2':{
        name:'厨余垃圾(湿垃圾)',
        img:'household_food_waste.png',
        logo: 'food_logo.png'
      },
      '3':{
        name:'其他垃圾(干垃圾)',
        img:'residual_waste.png',
        logo:'residual_logo.png'
      }
    },
  },
  getters:{
    wasteInfo: state=>type=>{
      return state.wasteType[type];
    }
  },
  mutations: {
    show: state => {
      state.alertShow = true;
    },
    hide: state => {
      state.alertShow = false;
    },
    setText: (state, text) => {
      state.alertText = text;
    },
    setType: (state, type) => {
      state.alertType = type;
    }
  },
  actions: {
    //显示警告，三秒后隐藏
    showAlert: ({ commit }, { text, type }) => {
      if (!type) {
        type = "error";
      }
      commit("setType", type);
      commit("setText", text);
      commit("show");
      setTimeout(() => {
        commit("hide");
      }, 3000);
    },
    //显示警告，不隐藏
    showAlertAlways: ({ commit }, { text, type }) => {
      if (!type) {
        type = "error";
      }
      commit("setType", type);
      commit("setText", text);
      commit("show");
    },
    //隐藏警告
    closeAlert: ({ commit }) => {
      commit("hide");
    }
  }
});
